﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IrisGenerator.Framework.Events;

namespace IrisGenerator.Common.Modules
{
    /// <summary>
    /// Base class for all Code Generators Module. All modules must inherit from this class.
    /// </summary>
    public abstract class ModuleBase:Module
    {
        public event EventHandler<ProgressEventArgs<ModuleBase>> AnalyseProgress;
        public event EventHandler<BaseEventArgs<ModuleBase>> StartingAnalyse;

        protected ModuleBase()
        {
            InitializeModule();
        }

        protected virtual void OnStartingAnalyse(BaseEventArgs<ModuleBase> e)
        {
            if (StartingAnalyse != null)
                StartingAnalyse(this, e);
        }

        protected virtual void OnAnalyseProgress(ProgressEventArgs<ModuleBase> e)
        {
            if (this.AnalyseProgress != null)
                AnalyseProgress(this, e);
        }

        public abstract bool Analyse();
        public abstract bool Generate();

        private Dictionary<string, ModuleParameter> _parameters;
        private Dictionary<string, ModuleParameterInstance> _parametersInstances;

        public Dictionary<string, ModuleParameter> Parameters
        {
            get
            {
                if (_parameters == null)
                    _parameters = new Dictionary<string, ModuleParameter>();
                return _parameters;
            }
        }

        protected void InitializeModule()
        {
            foreach (KeyValuePair<string, ModuleParameter> parameter in this.Parameters)
            {
                if (!this.ParametersInstances.ContainsKey(parameter.Key))
                {
                    this.ParametersInstances.Add(parameter.Key, null);
                }
            }
        }

        public Dictionary<string, ModuleParameterInstance> ParametersInstances
        {
            get
            {
                if (_parametersInstances == null)
                    _parametersInstances = new Dictionary<string, ModuleParameterInstance>();
                return _parametersInstances;
            }
        }



    }
}
